using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace LogAnalyzer.Grabbers.Dialogs
{
    public partial class SocketGrabberCfg : Form
    {

        public SocketGrabberCfg()
        {
            InitializeComponent();

            InitCombos();

            grpSocket.Text = Globalization.ResourceManager.Current.GetString("SOCKET");
            Text = Globalization.ResourceManager.Current.GetString("SOCKET_GRABBER_CONFIGURATION");
        }


        private void SelectInCombo(ComboBox cb, object item)
        {
            for (int i = 0; i < cb.Items.Count; i++)
            {
                object obj = cb.Items[i];
                if (obj.ToString() == item.ToString())
                {
                    cb.SelectedIndex = i;
                    return;
                }
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            IPAddress _tempIP;
            bool validIp = IPAddress.TryParse(txtIP.Text, out _tempIP);
            if (validIp == false)
            {
                MessageBox.Show("Invalid IP address.", "Socket grabber", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtIP.Focus();
                txtIP.SelectAll();
                return;
            }

            DialogResult = DialogResult.OK;
        }

        
   
        public Protocol Protocol
        {
            get { return (Protocol)Enum.Parse(typeof(Protocol), cmbProtocol.SelectedItem.ToString()); }
            set { SelectInCombo(cmbProtocol, value); }
        }

        public IPAddress IPAddress
        {
            get { return IPAddress.Parse(txtIP.Text); }
            set { txtIP.Text = value.ToString(); }
        }

        public int PortNumber
        {
            get { return (int)nudPort.Value; }
            set { nudPort.Value = value; }
        }

        public DataEncoding DataEncoding
        {
            get { return (DataEncoding)Enum.Parse(typeof(DataEncoding), cmbEncoding.SelectedItem.ToString()); }
            set { SelectInCombo(cmbEncoding, value); }
        }


        private void InitCombos()
        {
            foreach (string name in Enum.GetNames(typeof(Protocol)))
            {
                cmbProtocol.Items.Add(name);
            }

            foreach (string name in Enum.GetNames(typeof(DataEncoding)))
            {
                cmbEncoding.Items.Add(name);
            }

        }


    }
}